<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>商城管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/commodity' }"
        >商城列表</el-breadcrumb-item
      >
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <div class="form_title">
        <h5>添加商品</h5>
      </div>
      <!-- 添加商品表单 -->
      <el-form ref="form" :model="form" label-width="95px" :rules="formRules">
        <el-form-item label="名称：" prop="name">
          <el-col :span="11">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="上传图片：">
          <div class="uploadButton">
            <el-button size="small" type="primary" @click="showDialog"
              >点击上传</el-button
            ><span> 只能上传jpg/png文件 </span>
          </div>
          <el-image
            class="uploadImage"
            style="width: 100px; height: 100px"
            :src="url"
          ></el-image>
        </el-form-item>
        <el-form-item label="分类：">
          <el-col :span="12">
            <el-select
              v-model="form.typeId"
              filterable
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="口味：">
          <el-col :span="12">
            <el-select
              filterable
              multiple
              v-model="form.flavorId"
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
            >
              <el-option
                v-for="item in flavorList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="质量：" prop="quality">
          <el-col :span="11">
            <el-input
              v-model="form.quality"
              placeholder="单位需要写入如：5g"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="价格：">
          <el-col :span="11">
            <el-input v-model="price" type="number">
              <template slot="append">元</template>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="介绍：" prop="describe">
          <el-col :span="11">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.describe"
            >
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item class="state" label="状态：">
          <div class="stateRadio">
            <el-radio v-model="form.state" label="1">启用</el-radio>
            <el-radio v-model="form.state" label="2">禁用</el-radio>
          </div>
        </el-form-item>

        <el-form-item class="formbutton">
          <el-button
            class="formsubmit"
            type="primary"
            @click="onSubmit"
            :disabled="disabled"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 剪裁组件弹窗 -->
      <el-dialog
        title="裁切图片"
        :visible.sync="cropperModel"
        width="1200px"
        center
      >
        <cropper-image
          :Name="cropperName"
          ref="child"
          @uploadImgSuccess="handleUploadSuccess"
          :autoCropWidth="autoCropWidth"
          :autoCropHeight="autoCropHeight"
          :fixedNumber="fixedNumber"
        >
        </cropper-image>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import CropperImage from "@/components/CropperImage";
export default {
  components: { CropperImage },
  data() {
    return {
      //裁切图片参数
      cropperModel: false,
      cropperName: "",
      imgName: "",
      imgVisible: false,
      autoCropWidth: 300, //默认生成截图框宽度
      autoCropHeight: 260, //默认生成截图框高度
      fixedNumber: [1, 1],
      labelPosition: "right",
      typeList: [],
      flavorList: [],
      form: {
        name: "",
        image: "",
        memberId: "",
        //提交给后台的price
        price: "",
        state: "1",
        quality: "",
        typeId: "",
        flavorId: [],
        describe: "",
      },
      //前端显示的price
      price: "",
      formRules: {
        // 验证表单是否合法
        name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
        quality: [
          { required: true, message: "请输入商品质量", trigger: "blur" },
        ],
        describe: [
          { required: true, message: "请输入商品简述", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    showDialog() {
      this.cropperModel = true;
    },
    handleUploadSuccess(data) {
      console.log(data);
      // switch (data.name) {
      //   case "flagImg":
      //     this.formValidate.mainImage = "http://ydfblog.cn/dfs/" + data.url;
      //     console.log("最终输出" + data.name);
      //     break;
      // }
      this.form.image = data;
      this.url = this.$global.imgURL + data;
      this.cropperModel = false;
    },
    //得到商品分类列表
    async getTypes() {
      const { data: res } = await this.$http.get(
        "commodityType/getCommodityInState"
      );
      console.log(res);
      this.typeList = res.data.types;
      return;
    },
    //得到商品口味列表
    async getFlavors() {
      const { data: res } = await this.$http.get("Flavor/listFlavor");
      console.log(res);
      this.flavorList = res.data.flavors;
      return;
    },
    //提交调用相应的接口
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (this.form.typeId === "") {
          this.$message.error("请选择商品分类");
          return;
        }
        if (this.form.image === "") {
          this.$message.error("请上传图片");
          return;
        }
        if (this.price <= 0) {
          this.$message.error("请注意价格");
          return;
        }
        console.log("valid:" + valid);
        if (valid) {
          this.form.price = this.price * 100;
          console.log(this.form);
          console.log("submit!");
          // 调用提交接口
          const { data: res } = await this.$http.post(
            "commodity/addCommodity",
            this.form
          );
          if (res.code !== 200) {
            this.$message.error(res.massage);
            return;
          }
          this.$message.success("添加商品成功！");
          this.$router.replace("/commodity");
        } else {
          console.log("表单问题");
          return;
        }
      });
    },
  },
  created() {
    this.getTypes();
    this.getFlavors();
  },
};
</script>

<style lang="less" scoped>
.form_title {
  height: 40px;
  text-align: left;
  line-height: 0px;
  letter-spacing: 1px;
  border-left: rgba(64, 158, 255, 0.781) solid 3px;
  box-shadow: 0 3px 8px #ddd;
  padding-left: 10px;
  margin-top: 0px;
  overflow: hidden;
}

.el-form {
  width: 800px;
  margin: 50px auto;
  text-align: center;
  padding-left: 350px;
}
.formbutton,
.state {
  position: relative;
}
.formbutton .formsubmit,
.state .stateRadio {
  position: absolute;
  left: 80px;
}
</style>
